package com.fnst.facedetect.dao;

import java.io.Reader;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.fnst.facedetect.model.DetectedInfo;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class CommonDao {
	protected Logger log = Logger.getLogger(getClass());
	
	protected static SqlMapClient sqlMapClient;
	
	private static CommonDao instance;
	
	public static synchronized CommonDao getInstance() throws Exception{
		if(instance == null){
			instance = new CommonDao();
		}
		return instance;
	}
	
	public CommonDao() throws Exception{
		Reader reader = Resources.getResourceAsReader ("SqlMapConfig.xml");
		sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
	}
	
	public void addDetectedInfo(DetectedInfo d){
		try {
			long id = Long.valueOf(sqlMapClient.queryForObject("detect.getDetectedInfoID").toString());
			d.setId(id);
			sqlMapClient.insert("detect.insert_detectedInfo", d);
		} catch (SQLException e) {
			log.error("addDetectedInfo error", e);
			d.setId(0);
		}
	}
	
	public void updateDetectInfo(DetectedInfo d){
		try {
			sqlMapClient.update("detect.update_detectedInfo", d);
		} catch (Exception e) {
			log.error("addDetectedInfo error", e);
		}
	}
	
}
